package com.sgb.jicai.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.google.android.material.tabs.TabLayout;
import com.sgb.jicai.R;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.databinding.FrDemandJicaiChildBinding;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.sgb.jicai.entity.JiCaiHomeListRequestBean;
import com.sgb.jicai.view.adapter.CentralizedMiningCuttingLineAdapter;
import com.sgb.jicai.viewmodel.ChildForDemandJiCaiModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;
import java.util.List;

/**
 * Time:2021/5/16
 * AUTHOR:F_Q
 * Description:需求公告-集采需求
 */
public class ChildForDemandJiCaiFragment extends AppLazyFragment {
    ChildForDemandJiCaiModel viewModel;
    private FrDemandJicaiChildBinding binding;
    private String[] jicaitab = {"需方发起", "企业专项"};
    private String websiteNo;
    private JiCaiHomeListRequestBean bean;
    private CentralizedMiningCuttingLineAdapter centralizedMiningAdapter;

    public static ChildForDemandJiCaiFragment newInstance(String websiteNo) {
        ChildForDemandJiCaiFragment fragment = new ChildForDemandJiCaiFragment();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_demand_jicai_child, container, false);
        viewModel = new ViewModelProvider(this).get(ChildForDemandJiCaiModel.class);
        viewModel.setActivity(ChildForDemandJiCaiFragment.this);
        if (getArguments() != null) {
            websiteNo = getArguments().getString("websiteNo");
        }
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        bean = new JiCaiHomeListRequestBean();
        bean.setCompanyNo(websiteNo);
        bean.setGroupBuyMode("1");
        initRecycler();
        for (String jicainame : jicaitab) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText(jicainame));
        }
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 0) {
                    bean.setGroupBuyMode("1");
                } else {
                    bean.setGroupBuyMode("2");
                }
                initFilterData(false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    private void initRecycler() {
        binding.listView.setPullRefreshEnabled(true);
        binding.listView.setLoadingMoreEnabled(true);
        binding.listView.setLayoutManager(new LinearLayoutManager(getContext()));
        centralizedMiningAdapter = new CentralizedMiningCuttingLineAdapter(getActivity(), null);
        binding.listView.setAdapter(centralizedMiningAdapter);
        binding.listView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });
        centralizedMiningAdapter.setOnItemClick(new AdapterOnItemClick<CentralizedMiningEntity>() {
            @Override
            public void onItemClick(CentralizedMiningEntity entity, int position) {
                viewModel.getH5Url(entity.getNo());
            }
        });
    }

    private void initFilterData(boolean isLoadMore) {
        viewModel.getJiCaiList(bean, isLoadMore);
    }

    @Override
    protected void onLazyLoad() {
        initFilterData(false);
    }

    //集采list数据
    public void showList(List<CentralizedMiningEntity> list, boolean isLoadMore, boolean noMoreData) {
        binding.listView.refreshComplete();
        binding.listView.loadMoreComplete();
        binding.listView.setNoMore(noMoreData);
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                centralizedMiningAdapter.addDatas(list);
            }else {
                binding.listView.setNoMore(true);
            }
        } else {
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                binding.listView.setVisibility(View.VISIBLE);
                centralizedMiningAdapter.setDatas(list);
                binding.listView.setNoMore(false);
            } else {
                centralizedMiningAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.listView.setVisibility(View.GONE);
            }
        }
    }

    //跳转到详情信息界面
    public void showUrl(String url) {
        JiCaiBaseApi.startWeb(getActivity(), url);
    }
}
